package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.MyItemType;
import com.ruoyi.system.mapper.MyItemTypeMapper;
import com.ruoyi.system.service.IMyItemTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 物料产品分类Service业务层处理
 *
 * @author t382
 * @date 2025-04-29
 */
@Service
public class MyItemTypeServiceImpl implements IMyItemTypeService {
    @Autowired
    private MyItemTypeMapper myItemTypeMapper;


    @Override
    public String generateItemTypeCode() {
        // 查询当前最大编号
        Integer maxNum = myItemTypeMapper.selectMaxCodeNumber();
        int nextNum = (maxNum == null) ? 1 : maxNum + 1;
        return String.format("ITEM_TYPE_%04d", nextNum);
    }

    /**
     * 查询物料产品分类
     *
     * @param itemTypeId 物料产品分类主键
     * @return 物料产品分类
     */
    @Override
    public MyItemType selectMyItemTypeByItemTypeId(Long itemTypeId) {
        return myItemTypeMapper.selectMyItemTypeByItemTypeId(itemTypeId);
    }

    /**
     * 查询物料产品分类列表
     *
     * @param myItemType 物料产品分类
     * @return 物料产品分类
     */
    @Override
    public List<MyItemType> selectMyItemTypeList(MyItemType myItemType) {
        return myItemTypeMapper.selectMyItemTypeList(myItemType);
    }

    /**
     * 新增物料产品分类
     *
     * @param myItemType 物料产品分类
     * @return 结果
     */
    @Override
    public int insertMyItemType(MyItemType myItemType) {
        myItemType.setCreateTime(DateUtils.getNowDate());
        // 生成分类编码
        String code = this.generateItemTypeCode();
        myItemType.setItemTypeCode(code);

        // 获取父分类的 ancestors
        Long parentId = myItemType.getParentTypeId();
        String parentAncestors = "0"; // 默认根节点

        if (parentId != null && parentId != 0L) {
            MyItemType parentType = myItemTypeMapper.selectMyItemTypeByItemTypeId(parentId);
            if (parentType != null) {
                parentAncestors = parentType.getAncestors();
                // 设置当前分类的 ancestors
                myItemType.setAncestors(parentAncestors + "," + parentId);
            }
        } else if (parentId == 0L) {
            // 设置当前分类的 ancestors
            myItemType.setAncestors(parentAncestors);
        }


        // 插入数据库
        return myItemTypeMapper.insertMyItemType(myItemType);
    }

    /**
     * 修改物料产品分类
     *
     * @param myItemType 物料产品分类
     * @return 结果
     */
    @Override
    public int updateMyItemType(MyItemType myItemType) {
        myItemType.setUpdateTime(DateUtils.getNowDate());
        return myItemTypeMapper.updateMyItemType(myItemType);
    }

    /**
     * 批量删除物料产品分类
     *
     * @param itemTypeIds 需要删除的物料产品分类主键
     * @return 结果
     */
    @Override
    public int deleteMyItemTypeByItemTypeIds(Long[] itemTypeIds) {
        return myItemTypeMapper.deleteMyItemTypeByItemTypeIds(itemTypeIds);
    }

    /**
     * 删除物料产品分类信息
     *
     * @param itemTypeId 物料产品分类主键
     * @return 结果
     */
    @Override
    public int deleteMyItemTypeByItemTypeId(Long itemTypeId) {
        return myItemTypeMapper.deleteMyItemTypeByItemTypeId(itemTypeId);
    }
}
